package com.ouyy.volunteerservice.volunteer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ouyy.volunteerservice.volunteer.domain.po.VolunteerRating;
import com.ouyy.volunteerservice.volunteer.service.VolunteerRatingService;
import com.ouyy.volunteerservice.volunteer.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/volunteer-rating")
public class VolunteerRatingController {

    @Autowired
    private VolunteerRatingService volunteerRatingService;

    /**
     * 查询所有评分（支持分页）
     */
    @GetMapping("/list")
    public List<VolunteerRating> list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return volunteerRatingService.page(new Page<>(pageNum, pageSize)).getRecords();
    }

    /**
     * 根据 ID 查询单个评分
     */
    @GetMapping("/{id}")
    public VolunteerRating getById(@PathVariable Integer id) {
        return volunteerRatingService.getById(id);
    }

    /**
     * 添加评分
     */
    @PostMapping("/add")
    public boolean add(@RequestBody VolunteerRating volunteerRating) {
        return volunteerRatingService.save(volunteerRating);
    }

    /**
     * 更新评分
     */
    @PutMapping("/update")
    public boolean update(@RequestBody VolunteerRating volunteerRating) {
        return volunteerRatingService.updateById(volunteerRating);
    }

    /**
     * 根据 ID 删除评分
     */
    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable Integer id) {
        return volunteerRatingService.removeById(id);
    }

    /**
     * 根据志愿者 ID 查询评分
     */
    @GetMapping("/by-volunteer/{volunteerId}")
    public List<VolunteerRating> getByVolunteerId(@PathVariable Integer volunteerId) {
        return volunteerRatingService.list(new QueryWrapper<VolunteerRating>().eq("volunteer_id", volunteerId));
    }

    /**
     * 根据组织 ID 查询评分
     */
    @GetMapping("/by-organization/{organizationId}")
    public ResponseData<List<VolunteerRating>> getByOrganizationId(@PathVariable Integer organizationId) {
        // 获取评分数据
        List<VolunteerRating> ratings = volunteerRatingService.list(new QueryWrapper<VolunteerRating>()
                .eq("organization_id", organizationId));

        // 返回统一响应格式
        if (ratings.isEmpty()) {
            return new ResponseData<>("success", ratings, "No ratings found for this organization.");
        } else {
            return new ResponseData<>("success", ratings, "Ratings retrieved successfully.");
        }
    }



    @GetMapping("/by-organization/{organizationId}/{volunteerId}")
    public ResponseData<List<VolunteerRating>> getByOrganizationIdandVoluneerId(@PathVariable Integer organizationId, @PathVariable Integer volunteerId) {
        // 获取评分数据
        List<VolunteerRating> ratings = volunteerRatingService.list(new QueryWrapper<VolunteerRating>()
                .eq("organization_id", organizationId)
                .eq("volunteer_id", volunteerId)
        );

        // 返回统一响应格式
        if (ratings.isEmpty()) {
            return new ResponseData<>("success", ratings, "No ratings found for this organization.");
        } else {
            return new ResponseData<>("success", ratings, "Ratings retrieved successfully.");
        }
    }
}
